package com.linln.modules.message.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import com.linln.common.constant.StatusConst;
import com.linln.modules.message.domain.MessageInfo;
import com.linln.modules.system.repository.BaseRepository;

/**
 * @author hct
 * @date 2019/09/12
 */
public interface MessageInfoRepository extends JpaRepository<MessageInfo, Long>, JpaSpecificationExecutor<MessageInfo>, BaseRepository<MessageInfo, Long>{
	
	@Modifying
	@Transactional
	@Query("update MessageInfo set messageStatus = ?1  where id = ?2 ")
	public Integer updateMessageStatus(Integer storeStatus, Long id);
	@Modifying
	@Transactional
	@Query("update MessageInfo set messageStatus = 2,pay_status = 2  where id = ?1 ")
	public Integer downMessageStatus(Long id);

	@Modifying
	@Transactional
	@Query("update MessageInfo set readnum = readnum+1  where id = ?1 ")
	public Integer updateIsReadAddById(Long id);
	 
	@Modifying
	@Transactional
	@Query("update MessageInfo set collectnum = collectnum+1  where id = ?1 ")
	public Integer updateIsCollectAddById(Long id);
	
	@Modifying
	@Transactional
	@Query("update MessageInfo set collectnum = collectnum-1  where id = ?1 ")
	public Integer updateIsCollectSubById(Long id);
	
	@Modifying
	@Transactional
	@Query("update MessageInfo set zannum = zannum+1  where id = ?1 ")
	public Integer updateIsZanAddById(Long id);
	
	@Modifying
	@Transactional
	@Query("update MessageInfo set zannum = zannum-1  where id = ?1 ")
	public Integer updateIsZanSubById(Long id);
	
	
    @Transactional
	@Query(value="select count(*)  from MessageInfo where area = ?1 ")
	public Integer getCountByArea(String area);
    
    @Transactional
	@Query(value="select count(*)  from MessageInfo where area = ?1 and to_days(create_date) = to_days(now()) ")
	public Integer getCountByAreaAndToday(String area);
    
    
    
    //展示置顶时间的数据情况   
    @Transactional
	@Query(value="select * from  messageinfo where  area=?1 and istop=1 and message_status=1 and now()<end_time and now() > start_time and DATE_FORMAT(start_time,\"%H:%i\")< DATE_FORMAT(now(),\"%H:%i\")\r\n" + 
			"and DATE_FORMAT(date_add(start_time, interval deadline minute),\"%H:%i\")> DATE_FORMAT(now(),\"%H:%i\") order by id desc ",nativeQuery=true)
	public List<MessageInfo> queryListByArea(String area);
    
    @Query(value="select * from  messageinfo where  area=?1 and message_type1=?2 and istop=1 and message_status=1 and now()<end_time and now() > start_time and DATE_FORMAT(start_time,\"%H:%i\")< DATE_FORMAT(now(),\"%H:%i\")\r\n" + 
			"and DATE_FORMAT(date_add(start_time, interval deadline minute),\"%H:%i\")> DATE_FORMAT(now(),\"%H:%i\") order by id desc  ",nativeQuery=true)
	public List<MessageInfo> queryListByArea(String area,Long type);
    
    
  //展示置顶时间的数据情况   
    @Transactional
	@Query(value="select * from  messageinfo where  area=?1 and istop=1 and message_status=1 and now() < end_time and now() > start_time order by id desc  ",nativeQuery=true)
	public List<MessageInfo> queryTopDayListByArea(String area);


    
    //展示置顶时间的数据情况   
    @Transactional
	@Query(value="select * from  messageinfo where  area=?1 and istop=1 and message_status=1 and message_type1=?2 and now() < end_time and now() > start_time order by id desc  ",nativeQuery=true)
	public List<MessageInfo> queryTopDayListByArea(String area,Long type);

    
    public  MessageInfo findByOrderId(String orderId);

    /**
	  * 确认已经支付，修改状态
	  * @param orderId
	  * @return
	  */
   @Modifying
   @Transactional
   @Query("update MessageInfo set messageStatus=1,payStatus =1  where orderId = ?1 and payStatus=0")
   public Integer updatePayStatus( String orderId);
   
   /**
	  * 退款，修改状态
	  * @param orderId
	  * @return
	  */
 @Modifying
 @Transactional
 @Query("update MessageInfo set payStatus =2  where orderId = ?1 and payStatus=1")
 public Integer updateRefundStatus( String orderId);

	/**
	 * 修改标题
	 * @param id
	 * @param title
	 * @return
	 */
 @Modifying
 @Transactional
 @Query("update  MessageInfo  set title = ?2  where id = ?1")
 public Integer updateMessageInfotitle(@Param("id")Long id, @Param("title") String title);

	/**
	 * 修改描述
	 * @param id
	 * @param details
	 * @return
	 */
	@Modifying
	@Transactional
 @Query("update  MessageInfo  set details = ?2  where id = ?1")
 public Integer updateMessageInfodetails(@Param("id")Long id,@Param("details") String details);

	@Modifying
	@Transactional
	@Query("update  MessageInfo  set logo = ?2  where id = ?1")
	public Integer updateMessageInfoLogo(@Param("id")Long id,@Param("logo") String logo);

	@Modifying
	@Transactional
	@Query("update  MessageInfo  set imgs = ?2  where id = ?1")
	public Integer updateMessageInfoImgs(@Param("id")Long id,@Param("logo") String imgs);

	@Modifying
	@Transactional
	@Query("update  MessageInfo  set startTime = ?2  where id = ?1")
	public Integer updateMessageInfoStartTime(@Param("id")Long id,@Param("startTime") Date startTime);
 
 @Query(value="select count(*) from messageinfo  where member_id = ?1  and to_days(create_date) = to_days(now()) " + 
 		" and  istop=0 and status <> " + StatusConst.DELETE,nativeQuery=true)
 public long getDayAddCount(Long userId);

	@Modifying
	@Query(value = "SELECT * FROM messageinfo WHERE istop = 1 AND end_time < NOW()",nativeQuery = true)
	public List<MessageInfo> overdueTopList();

	@Modifying
	@Transactional
	@Query("UPDATE MessageInfo SET  istop = 0 WHERE id = ?1 ")
	public Integer leaveTopById(Long id);
}